package cn.group7.padserver.service.impl;

import cn.group7.padserver.dao.AddressDao;
import cn.group7.padserver.pojo.Address;
import cn.group7.padserver.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 谷汉斌
 * @description
 * @createTime 2024/8/5 上午11:51
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;

    @Override
    public boolean add(Address address ) {
        //获取登录的用户名
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails principal = (UserDetails) authentication.getPrincipal();
        address.setOwnName(principal.getUsername());
        //默认不是默认地址
        address.setIsDefault(1);
        return addressDao.add(address);
    }

    @Override
    public List<Address> selectAll() {
        //获取登录的用户名
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails principal = (UserDetails) authentication.getPrincipal();
        String username = principal.getUsername();
        return addressDao.selectAll(username);
    }

    @Override
    public boolean delete(Integer id) {
        return addressDao.delete(id);
    }

    @Override
    public boolean update() {
        return addressDao.update();
    }

}
